/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("yjy_whole_data_analysis",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using VOL.Finance.IServices;
using Microsoft.AspNetCore.Authorization;
using VOL.Core.Filters;
using VOL.Core.DBManager;
using System.Linq;
using VOL.Core.ManageUser;
using Castle.Core.Internal;

namespace VOL.Finance.Controllers
{
    public partial class yjy_whole_data_analysisController
    {
        private readonly Iyjy_whole_data_analysisService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Iyjy_whole_costService _wholeCostService;
        private readonly yjy_whole_costController _wholeCostController;

        [ActivatorUtilitiesConstructor]
        public yjy_whole_data_analysisController(
            Iyjy_whole_data_analysisService service,
            IHttpContextAccessor httpContextAccessor,
            Iyjy_whole_costService _wholeCostService
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }

        [ApiActionPermission()]//权限重写，在控制器中增加
        [HttpPost, Route("getDataAnalysis"), AllowAnonymous]
        public dynamic getDataAnalysis(string yjy_whole_info_id)
        {
            yjy_whole_info info = DBServerProvider.DbContext.Set<yjy_whole_info>().Where(x => x.yjy_whole_info_id == Guid.Parse(yjy_whole_info_id)).FirstOrDefault();
            if (info == null)
            {
                return "参数出错，请联系管理员！";
            }
            if (info.status==0)
            {
                List<yjy_whole_cost> listCost = DBServerProvider.DbContext.Set<yjy_whole_cost>().Where(x => x.materialnum == info.materialnum && x.yjy_whole_info_id == Guid.Parse(yjy_whole_info_id)).ToList();
                List<yjy_whole_wlzb> listWlzb = DBServerProvider.DbContext.Set<yjy_whole_wlzb>().Where(x => x.materialnum == info.materialnum && x.tree_id == 1).ToList();
                string sql = "SELECT componentnum FROM (SELECT a.materialnum,a.componentnum ,a.description AS componentnum_name FROM yjy_whole_wlzb a WHERE a.tree_id = 1 AND a.materialnum = @_materialnum UNION ALL SELECT b.materialnum,b.componentnum ,b.componentnum_name FROM yjy_whole_cost b WHERE b.materialnum = @_materialnum) h GROUP BY componentnum";
                List<string> listTotal = DBServerProvider.SqlDapper.QueryList<string>(sql, new { _materialnum = info.materialnum }).ToList();
                List<yjy_whole_data_analysis> listAnalysis = new List<yjy_whole_data_analysis>();
                if (listCost.Count==0||listWlzb.Count==0)
                {
                    return "请上传整车成本数据！";
                }
                string materialnum_name = listCost[0].materialnum_name;
                foreach (string str in listTotal)
                {
                    int i = 0, j = 0;
                    yjy_whole_data_analysis analysis = new yjy_whole_data_analysis();
                    analysis.materialnum = info.materialnum;
                    analysis.materialnum_name = materialnum_name;
                    foreach (var item in listCost)
                    {
                        if (str.Equals(item.componentnum))
                        {
                            analysis.componentnum = item.componentnum;
                            analysis.componentnum_name = item.componentnum_name;
                            i = 1;
                            break;
                        }
                    }
                    foreach (var item1 in listWlzb)
                    {
                        if (str.Equals(item1.componentnum))
                        {
                            analysis.sap_componentnum = item1.componentnum;
                            analysis.sap_componentnum_name = item1.description;
                            j = 1;
                            break;
                        }
                    }
                    if (i == 1 && j == 1)
                    {
                        analysis.status = 1;
                    }
                    else
                    {
                        analysis.status = 2;
                    }
                    analysis.Creator = UserContext.Current.UserName;
                    analysis.CreateDate = DateTime.Now;
                    analysis.yjy_whole_info_id = Guid.Parse(yjy_whole_info_id);
                    DBServerProvider.DbContext.Add(analysis);
                }
                info.status = 1;
                DBServerProvider.DbContext.Update(info);
                DBServerProvider.DbContext.SaveChanges();
                return "整车数据分析生成成功！";
            }
            else
            {
                return "整车数据分析无需再生成！";
            }
            
        }
    }
}
